/**
 * Copyright (c) 2011 - 2012 National Library of Medicine
 *
 * Contact: tomasz.adamusiak@nih.gov;
 * 
 * This file is part of LOwlINC
 * 
 * LOwlINC is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * LOwlINC is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along
 * with LOwlINC. If not, see <http://www.gnu.org/licenses/>.
 */
package gov.nih.nlm.lowlinc.csv;

import gov.nih.nlm.lowlinc.accessionwrapper.LOINCPartID;
import gov.nih.nlm.lowlinc.accessionwrapper.LOINCTermID;
import gov.nih.nlm.lowlinc.bean.LOINCPartLinkBean;
import gov.nih.nlm.lowlinc.core.LowlincConfig;
import gov.nih.nlm.lowlinc.owlapi.LowlincException;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import au.com.bytecode.opencsv.CSVReader;

/**
 * @author Tomasz Adamusiak
 * 
 */
public class LOINCPartLinksCSVParser {
	private static final Logger log = Logger
			.getLogger(LOINCPartLinksCSVParser.class);
	private String[] headerCSV;
	private final Map<LOINCTermID, List<LOINCPartID>> links = new HashMap<>();

	public Map<LOINCTermID, List<LOINCPartID>> getLinks() {
		return links;
	}

	public LOINCPartLinksCSVParser() throws LowlincException {
		try {

			Integer counter = -1; // skip header
			CSVReader reader = new CSVReader(
					new FileReader(new File(
							LowlincConfig
									.getString("LOINCPartLinksCSVParser.path"))), '|'); //$NON-NLS-1$
			headerCSV = reader.readNext();
			String[] nextLine;

			while ((nextLine = reader.readNext()) != null) {
				counter++;
				LOINCPartLinkBean link = bindStringToBean(nextLine);
				LOINCTermID termID = link.getTermID();
				LOINCPartID partID = link.getPartID();

				if (links.containsKey(termID)) {
					links.get(termID).add(partID);
				} else {
					List<LOINCPartID> parts = new ArrayList<>();
					parts.add(partID);
					links.put(termID, parts);
				}
			}
			log.info(counter + " LOINC Part Links loaded for " + links.size()
					+ " terms");
		} catch (IOException e) {
			throw new LowlincException(e);
		}
	}

	private LOINCPartLinkBean bindStringToBean(String[] line)
			throws LowlincException {
		try {
			LOINCPartLinkBean bean = new LOINCPartLinkBean();

			for (int i = 0; i < headerCSV.length; i++) {
				Method meth = bean.getClass().getMethod("set" + headerCSV[i],
						String.class);
				meth.invoke(bean, line[i]);
			}
			return bean;

		} catch (NoSuchMethodException e) {
			throw new LowlincException(e);
		} catch (SecurityException e) {
			throw new LowlincException(e);
		} catch (IllegalAccessException e) {
			throw new LowlincException(e);
		} catch (IllegalArgumentException e) {
			throw new LowlincException(e);
		} catch (InvocationTargetException e) {
			throw new LowlincException(e);
		}
	}
}
